package com.ibeeking.found.content.b.rest.service.impl;

import cn.binarywang.wx.miniapp.bean.live.WxMaCreateRoomResult;
import cn.binarywang.wx.miniapp.bean.live.WxMaLiveResult;
import cn.binarywang.wx.miniapp.bean.live.WxMaLiveRoomInfo;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ibeeking.found.common.enums.PublishStatusEnum;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.content.b.rest.service.IBroadcastRoomService;
import com.ibeeking.found.content.b.rest.service.IWxBroadcastService;
import com.ibeeking.found.content.service.common.dos.BroadcastCouponDO;
import com.ibeeking.found.content.service.common.dos.BroadcastCouponRelationDO;
import com.ibeeking.found.content.service.common.dos.BroadcastDO;
import com.ibeeking.found.content.service.common.dos.BroadcastGoodsDO;
import com.ibeeking.found.content.service.common.dos.BroadcastGoodsRelationDO;
import com.ibeeking.found.content.service.common.dos.BroadcastGroupDO;
import com.ibeeking.found.content.service.common.param.BroadcastParam;
import com.ibeeking.found.content.service.common.query.BroadcastPageQuery;
import com.ibeeking.found.content.service.common.query.BroadcastQuery;
import com.ibeeking.found.content.service.common.query.BroadcastRoomIdPageQuery;
import com.ibeeking.found.content.service.common.vo.BroadcastCouponVO;
import com.ibeeking.found.content.service.common.vo.BroadcastGoodsVO;
import com.ibeeking.found.content.service.common.vo.BroadcastVO;
import com.ibeeking.found.content.service.mapper.BroadcastCouponMapper;
import com.ibeeking.found.content.service.mapper.BroadcastCouponRelationMapper;
import com.ibeeking.found.content.service.mapper.BroadcastGoodsMapper;
import com.ibeeking.found.content.service.mapper.BroadcastGoodsRelationMapper;
import com.ibeeking.found.content.service.mapper.BroadcastGroupMapper;
import com.ibeeking.found.content.service.mapper.BroadcastMapper;

import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName BroadcastServiceImpl
 * @Description 直播间
 * @Author ibeeking
 * @Date 2022-02-18 14:11
 **/
@Service
public class BroadcastRoomServiceImpl extends ServiceImpl<BroadcastMapper, BroadcastDO> implements IBroadcastRoomService {

    @Resource
    private BroadcastMapper broadcastMapper;
    @Resource
    private IWxBroadcastService wxBroadcastServiceImpl;
    @Resource
    private BroadcastGroupMapper broadcastGroupMapper;
    @Resource
    private BroadcastGoodsMapper broadcastGoodsMapper;
    @Resource
    private BroadcastCouponMapper broadcastCouponMapper;
    @Resource
    private BroadcastGoodsRelationMapper goodsRelationMapper;
    @Resource
    private BroadcastCouponRelationMapper couponRelationMapper;

    /***
     * 查询列表
     */
    @Override
    public List<BroadcastVO> list(BroadcastQuery broadcastQuery) {
        LambdaQueryWrapper<BroadcastDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(broadcastQuery.getRoomName()), BroadcastDO::getRoomName, broadcastQuery.getRoomName());
        // 2-已结束 1-直播中 0-未开始
        if (ObjectUtil.isNotNull(broadcastQuery.getLiveStatus())) {
            Date now = new Date();
            if (broadcastQuery.getLiveStatus().equals(0)) {
                // 开始直播时间大于当前时间
                queryWrapper.ge(BroadcastDO::getStartTime, now);
            } else if (broadcastQuery.getLiveStatus().equals(1)) {
                // 开始直播时间小于当前时间 结束直播时间大于当前时间
                queryWrapper.le(BroadcastDO::getStartTime, now);
                queryWrapper.ge(BroadcastDO::getEndTime, now);
            } else if (broadcastQuery.getLiveStatus().equals(2)) {
                // 结束时间小于当前时间
                queryWrapper.le(BroadcastDO::getEndTime, now);
            }
        }
        queryWrapper.orderByDesc(BroadcastDO::getStartTime, BroadcastDO::getCreateTime);
        List<BroadcastDO> broadcastDOs = broadcastMapper.selectList(queryWrapper);
        List<BroadcastVO> broadcastVOS = JsonUtils.jsonToList(broadcastDOs, BroadcastVO.class);
        for (BroadcastVO item : broadcastVOS) {
            Long groupId = item.getGroupId();
            BroadcastGroupDO broadcastGroupDO = broadcastGroupMapper.selectById(groupId);
            if (ObjectUtil.isNotNull(broadcastGroupDO)) {
                item.setGroupName(broadcastGroupDO.getName());
            } else {
                item.setGroupName(StringUtils.EMPTY);
            }
            item.setLiveStatus(getLiveStatus(item.getStartTime(), item.getEndTime()));
        }
        return broadcastVOS;
    }

    /**
     * 直播源查询
     *
     * @param broadcastQuery
     * @return
     */
    @Override
    public Map<String, List<BroadcastVO>> source(BroadcastQuery broadcastQuery) {
        List<BroadcastVO> list = list(broadcastQuery);
        return list.stream().collect(Collectors.groupingBy(BroadcastVO::getGroupName));
    }

    @Override
    public Page<BroadcastGoodsVO> getGoodsPageList(BroadcastRoomIdPageQuery pageQuery) {
        LambdaQueryWrapper<BroadcastGoodsRelationDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotNull(pageQuery.getRoomId()), BroadcastGoodsRelationDO::getRoomId, pageQuery.getRoomId());
        queryWrapper.orderByDesc(BroadcastGoodsRelationDO::getCreateTime);
        Page<BroadcastGoodsRelationDO> page = new Page<>();
        if (ObjectUtil.isNotNull(pageQuery.getPageNum()) && ObjectUtil.isNotNull(pageQuery.getPageSize())) {
            page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        }
        Page<BroadcastGoodsRelationDO> pages = goodsRelationMapper.selectPage(page, queryWrapper);
        List<BroadcastGoodsRelationDO> relationDoList = pages.getRecords();
        List<BroadcastGoodsVO> recordList = new ArrayList<>();
        for (BroadcastGoodsRelationDO item : relationDoList) {
            Long goodsId = item.getGoodsId();
            LambdaQueryWrapper<BroadcastGoodsDO> itemGoodsQueryWrapper = new LambdaQueryWrapper<>();
            itemGoodsQueryWrapper.eq(BroadcastGoodsDO::getGoodsId, goodsId);
            BroadcastGoodsDO broadcastGoodsDO = broadcastGoodsMapper.selectOne(itemGoodsQueryWrapper);
            BroadcastGoodsVO itemGoodsVo = BeanUtil.convertBean(broadcastGoodsDO, BroadcastGoodsVO.class);
            recordList.add(itemGoodsVo);
        }
        Page<BroadcastGoodsVO> broadcastGoodsVoPage = BeanUtil.convertPage(pages, BroadcastGoodsVO.class);
        broadcastGoodsVoPage.setRecords(recordList);
        return broadcastGoodsVoPage;
    }

    @Override
    public Page<BroadcastCouponVO> getCouponPageList(BroadcastRoomIdPageQuery pageQuery) {
        LambdaQueryWrapper<BroadcastCouponRelationDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotNull(pageQuery.getRoomId()), BroadcastCouponRelationDO::getRoomId, pageQuery.getRoomId());
        queryWrapper.orderByDesc(BroadcastCouponRelationDO::getCreateTime);
        Page<BroadcastCouponRelationDO> page = new Page<>();
        if (ObjectUtil.isNotNull(pageQuery.getPageNum()) && ObjectUtil.isNotNull(pageQuery.getPageSize())) {
            page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        }
        Page<BroadcastCouponRelationDO> pages = couponRelationMapper.selectPage(page, queryWrapper);
        List<BroadcastCouponRelationDO> relationDoList = pages.getRecords();
        List<BroadcastCouponVO> recordList = new ArrayList<>();
        for (BroadcastCouponRelationDO item : relationDoList) {
            Long couponId = item.getCouponId();
            BroadcastCouponVO itemGoodsVo = new BroadcastCouponVO();
            LambdaQueryWrapper<BroadcastCouponDO> itemCouponQueryWrapper = new LambdaQueryWrapper<>();
            itemCouponQueryWrapper.eq(BroadcastCouponDO::getCouponId, couponId);
            BroadcastCouponDO broadcastCouponDO = broadcastCouponMapper.selectOne(itemCouponQueryWrapper);
            BeanUtils.copyProperties(broadcastCouponDO, itemGoodsVo);
            recordList.add(itemGoodsVo);
        }
        Page<BroadcastCouponVO> broadcastCouponVoPage = BeanUtil.convertPage(pages, BroadcastCouponVO.class);
        broadcastCouponVoPage.setRecords(recordList);
        return broadcastCouponVoPage;
    }

    /***
     * 分页查询
     */
    @Override
    public Page<BroadcastVO> pageList(BroadcastPageQuery broadcastPageQuery) {
        LambdaQueryWrapper<BroadcastDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(broadcastPageQuery.getRoomName()), BroadcastDO::getRoomName, broadcastPageQuery.getRoomName());
        queryWrapper.ge(StringUtils.isNotBlank(broadcastPageQuery.getCreateTimeStart()), BroadcastDO::getCreateTime, broadcastPageQuery.getCreateTimeStart());
        queryWrapper.le(StringUtils.isNotBlank(broadcastPageQuery.getCreateTimeEnd()), BroadcastDO::getCreateTime, broadcastPageQuery.getCreateTimeEnd());
        // 2-已结束 1-直播中 0-未开始
        if (ObjectUtil.isNotNull(broadcastPageQuery.getLiveStatus())) {
            String now = DateUtil.now();
            if (broadcastPageQuery.getLiveStatus().equals(0)) {
                // 开始直播时间大于当前时间
                queryWrapper.ge(BroadcastDO::getStartTime, now);
            } else if (broadcastPageQuery.getLiveStatus().equals(1)) {
                // 开始直播时间小于当前时间 结束直播时间大于当前时间
                queryWrapper.le(BroadcastDO::getStartTime, now);
                queryWrapper.ge(BroadcastDO::getEndTime, now);
            } else if (broadcastPageQuery.getLiveStatus().equals(2)) {
                // 结束时间小于当前时间
                queryWrapper.le(BroadcastDO::getEndTime, now);
            }
        }
        queryWrapper.orderByDesc(BroadcastDO::getStartTime, BroadcastDO::getCreateTime);
        Page<BroadcastDO> page = new Page<>();
        if (ObjectUtil.isNotNull(broadcastPageQuery.getPageNum()) && ObjectUtil.isNotNull(broadcastPageQuery.getPageSize())) {
            page = new Page<>(broadcastPageQuery.getPageNum(), broadcastPageQuery.getPageSize());
        }
        Page<BroadcastDO> pages = broadcastMapper.selectPage(page, queryWrapper);
        Page<BroadcastVO> broadcastVoPage = BeanUtil.convertPage(pages, BroadcastVO.class);
        List<BroadcastDO> doRecords = pages.getRecords();
        List<BroadcastVO> broadcastVoList = JsonUtils.jsonToList(doRecords, BroadcastVO.class);
        for (BroadcastVO item : broadcastVoList) {
            Long groupId = item.getGroupId();
            String startTime = item.getStartTime();
            String endTime = item.getEndTime();
            if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
                String interval = DateUtil.between(DateUtil.parseDateTime(startTime), DateUtil.parseDateTime(endTime), DateUnit.SECOND) + "";
                String intervalStr = DateUtil.secondToTime(Integer.parseInt(interval));
                item.setIntervalLength(intervalStr);
            }
            BroadcastGroupDO broadcastGroupDO = broadcastGroupMapper.selectById(groupId);
            if (ObjectUtil.isNotNull(broadcastGroupDO)) {
                item.setGroupName(broadcastGroupDO.getName());
            }
            item.setLiveStatus(getLiveStatus(item.getStartTime(), item.getEndTime()));
        }
        broadcastVoPage.setRecords(broadcastVoList);
        return broadcastVoPage;
    }

    /***
     * 新增
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(BroadcastParam broadcastParam) {
        checkUnique(broadcastParam);
        checkParam(broadcastParam);
        BroadcastDO broadcastDO = BeanUtil.convertBean(broadcastParam, BroadcastDO.class);
        broadcastDO.setCreateBy(UserUtils.getUserId());
        broadcastDO.setCreateTime(LocalDateTime.now());
        broadcastDO.setModifyTime(LocalDateTime.now());
        broadcastDO.setPublishStatus(PublishStatusEnum.DISABLE.getValue());
        int count = broadcastMapper.insert(broadcastDO);
        if (count > 0) {
            Long id = broadcastDO.getId();
            BroadcastDO upDo = new BroadcastDO();
            upDo.setId(id);
            upDo.setPagePath("?room_id=" + id);
            broadcastMapper.updateById(upDo);
        }
        return count > 0;
    }

    /***
     * 修改
     */
    @Override
    public Boolean modify(BroadcastParam broadcastParam) {
        checkUnique(broadcastParam);
        checkParam(broadcastParam);
        BroadcastDO broadcastDO = BeanUtil.convertBean(broadcastParam, BroadcastDO.class);
        broadcastDO.setModifyBy(UserUtils.getUserId());
        broadcastDO.setModifyTime(LocalDateTime.now());
        return broadcastMapper.updateById(broadcastDO) > 0;
    }

    /***
     * 通过id查询
     */
    @Override
    public BroadcastVO view(Long id) {
        BroadcastDO broadcastDO = broadcastMapper.selectById(id);
        BroadcastVO broadcastVO = JsonUtils.fromJson(broadcastDO, BroadcastVO.class);
        broadcastVO.setLiveStatus(getLiveStatus(broadcastVO.getStartTime(), broadcastVO.getEndTime()));
        return broadcastVO;
    }

    /***
     * 发布状态
     */
    @Override
    public Integer changePublishStatus(Long id, Integer status) {
        // 只有点发布时候，才能把直播间信息推送到微信直播平台开始创建
        if (PublishStatusEnum.ENABLE.getValue().equals(status)) {
            return releaseRoom(id);
        } else {
            return offlineRoom(id);
        }
    }

    /**
     * 直播间下线
     *
     * @param id
     * @return
     */
    public Integer offlineRoom(Long id) {
        BroadcastDO broadcastDO = broadcastMapper.selectById(id);
        if (ObjectUtil.isNull(broadcastDO)) {
            throw new BusinessException("直播间信息不存在");
        }
        if (ObjectUtil.isNotNull(broadcastDO.getPublishStatus()) && broadcastDO.getPublishStatus().equals(PublishStatusEnum.DISABLE.getValue())) {
            // 直播间状态已经是下架状态
            throw new BusinessException("直播间已经是下架状态");
        }
        LocalDateTime endTime = broadcastDO.getEndTime();
        long endTimeMilli = endTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        if (endTimeMilli < DateUtil.current()) {
            // 直播间已经自动结束
            throw new BusinessException("直播间结束时间已过 自动结束");
        }
        // 如果下架，则删除微信直播平台的直播间
        Integer roomId = broadcastDO.getRoomId();
        WxMaLiveResult.RoomInfo roomInfo = wxBroadcastServiceImpl.getLiveRoomInfo(roomId);
        if (roomInfo == null) {
            throw new BusinessException("在微信直播平台下架直播间失败");
        }
        boolean result = wxBroadcastServiceImpl.deleteBroadcastRoom(roomId);
        if (!result) {
            throw new BusinessException("在微信直播平台下架直播间失败");
        }
        broadcastDO.setPublishStatus(PublishStatusEnum.DISABLE.getValue());
        int upStatus = broadcastMapper.updateById(broadcastDO);
        if (upStatus > 0) {
            return roomId;
        } else {
            throw new BusinessException("直播间状态更新失败");
        }
    }

    /**
     * 直播间发布
     *
     * @param id
     */
    public Integer releaseRoom(Long id) {
        BroadcastDO broadcastDO = broadcastMapper.selectById(id);
        checkParam(broadcastDO);

        WxMaLiveRoomInfo wxMaLiveRoomInfo = new WxMaLiveRoomInfo();
        wxMaLiveRoomInfo.setAnchorName(broadcastDO.getAnchorName());
        wxMaLiveRoomInfo.setAnchorWechat(broadcastDO.getAnchorChat());
        wxMaLiveRoomInfo.setCloseComment(broadcastDO.getOpenComment());
        wxMaLiveRoomInfo.setCloseGoods(broadcastDO.getOpenGoods());
        wxMaLiveRoomInfo.setCloseKf(broadcastDO.getOpenKf());
        wxMaLiveRoomInfo.setCloseLike(broadcastDO.getOpenLike());
        wxMaLiveRoomInfo.setCloseReplay(broadcastDO.getOpenReplay());
        wxMaLiveRoomInfo.setCloseShare(broadcastDO.getOpenShare());
        wxMaLiveRoomInfo.setCoverImg(broadcastDO.getCoverImgs());
        wxMaLiveRoomInfo.setFeedsImg(broadcastDO.getFeedsImgs());
        wxMaLiveRoomInfo.setEndTime(Timestamp.valueOf(broadcastDO.getEndTime()).getTime() / 1000);
        wxMaLiveRoomInfo.setType(broadcastDO.getLiveType());
        wxMaLiveRoomInfo.setName(broadcastDO.getRoomName());
        wxMaLiveRoomInfo.setStartTime(Timestamp.valueOf(broadcastDO.getStartTime()).getTime() / 1000);
        wxMaLiveRoomInfo.setShareImg(broadcastDO.getShareImgs());
        wxMaLiveRoomInfo.setAnchorImg(broadcastDO.getShareImgs());
        WxMaCreateRoomResult wxMaCreateRoomResult = wxBroadcastServiceImpl.createBroadcastRoom(wxMaLiveRoomInfo);
        if ((wxMaCreateRoomResult == null) || (wxMaCreateRoomResult.getRoomId() <= 0)) {
            throw new BusinessException("在微信直播平台发布直播间失败");
        }
        Integer roomId = wxMaCreateRoomResult.getRoomId();
        broadcastDO.setRoomId(wxMaCreateRoomResult.getRoomId());
        broadcastDO.setQrcodeUrl(wxMaCreateRoomResult.getQrcodeUrl());
        broadcastDO.setPublishStatus(PublishStatusEnum.ENABLE.getValue());
        broadcastDO.setPublishTime(LocalDateTime.now());
        broadcastDO.setModifyTime(LocalDateTime.now());
        broadcastDO.setModifyTime(LocalDateTime.now());
        broadcastDO.setModifyBy(UserUtils.getUserId());
        int upStatus = broadcastMapper.updateById(broadcastDO);
        if (upStatus > 0) {
            return roomId;
        } else {
            throw new BusinessException("直播间状态更新失败");
        }
    }

    /***
     * 通过id删除
     */
    @Override
    public Boolean del(Long id) {
        return broadcastMapper.deleteById(id) > 0;
    }

    /**
     * 校验唯一性
     *
     * @param param
     */
    public void checkUnique(BroadcastParam param) {
        LambdaQueryWrapper<BroadcastDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(ObjectUtil.isNotNull(param.getId()), BroadcastDO::getId, param.getId());
        queryWrapper.eq(StringUtils.isNotBlank(param.getRoomName()), BroadcastDO::getRoomName, param.getRoomName());
        List<BroadcastDO> dos = broadcastMapper.selectList(queryWrapper);
        if (Collections3.isNotEmpty(dos)) {
            throw new BusinessException("已经存在相同名字的直播间!");
        }
    }

    /**
     * 参数合法性校验
     *
     * @param param
     */
    public void checkParam(BroadcastParam param) {
        // 开播时间需要在当前时间的10分钟后 并且 开始时间不能在 6 个月后
        // 开播时间和结束时间间隔不得短于30分钟，不得超过24小时
        LocalDateTime startTime = param.getStartTime();
        long startTimeMilli = startTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        LocalDateTime endTime = param.getEndTime();
        long endTimeMilli = endTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();

        Date date = new Date();
        DateTime offsetMonthDate = DateUtil.offsetMonth(date, 6);
        DateTime offsetMinuteTime = DateUtil.offsetMinute(date, 10);
        if (startTimeMilli < offsetMinuteTime.getTime() || startTimeMilli > offsetMonthDate.getTime()) {
            throw new BusinessException("开播时间需要在当前时间的10分钟后 并且 开始时间不能在 6 个月后");
        }
        if (startTimeMilli >= endTimeMilli) {
            throw new BusinessException("结束时间不能小于开播时间");
        }
        if (endTimeMilli - startTimeMilli < 1800000 || endTimeMilli - startTimeMilli >= 86400000) {
            throw new BusinessException("开播时间和结束时间间隔不得短于30分钟，不得超过24小时");
        }
    }

    /**
     * 参数合法性校验
     *
     * @param broadcastDO
     */
    public void checkParam(BroadcastDO broadcastDO) {
        // 开播时间需要在当前时间的10分钟后 并且 开始时间不能在 6 个月后
        // 开播时间和结束时间间隔不得短于30分钟，不得超过24小时
        LocalDateTime startTime = broadcastDO.getStartTime();
        long startTimeMilli = startTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        LocalDateTime endTime = broadcastDO.getEndTime();
        long endTimeMilli = endTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();

        Date date = new Date();
        DateTime offsetMonthDate = DateUtil.offsetMonth(date, 6);
        DateTime offsetMinuteTime = DateUtil.offsetMinute(date, 10);
        if (startTimeMilli < offsetMinuteTime.getTime() || startTimeMilli > offsetMonthDate.getTime()) {
            throw new BusinessException("开播时间需要在当前时间的10分钟后 并且 开始时间不能在 6 个月后");
        }
        if (startTimeMilli >= endTimeMilli) {
            throw new BusinessException("结束时间不能小于开播时间");
        }
        if (endTimeMilli - startTimeMilli < 1800000 || endTimeMilli - startTimeMilli >= 86400000) {
            throw new BusinessException("开播时间和结束时间间隔不得短于30分钟，不得超过24小时");
        }
        if (ObjectUtil.isNotNull(broadcastDO.getPublishStatus()) && broadcastDO.getPublishStatus().equals(1)) {
            throw new BusinessException("直播间状态已经公开，请勿重复操作");
        }
    }

    /**
     * 获取直播间开播状态
     *
     * @param startTime
     * @param endTime
     */
    public Integer getLiveStatus(String startTime, String endTime) {
        DateTime startDateTime = DateUtil.parseDateTime(startTime);
        DateTime endDateTime = DateUtil.parseDateTime(endTime);

        long startDateTimeMillis = startDateTime.getTime();
        long endDateTimeMillis = endDateTime.getTime();
        long now = System.currentTimeMillis();
        // 直播状态 2-已结束 1-直播中 0-未开始
        if (startDateTimeMillis > now) {
            // 当前时间 小于 开始时间 未开始
            return 0;
        } else if (startDateTimeMillis < now && now < endDateTimeMillis) {
            // 当前时间大于开始时间小于结束时间 进行中
            return 1;
        } else {
            // 已结束
            return 2;
        }
    }
}
